import component from "element-plus/es/components/tree-select/src/tree-select-option.mjs";
import { createRouter, createWebHashHistory } from "vue-router";


const routes = [
    {
        path: "/",
        name: "indes",
        component: () => import("../page/indes/indes.vue") // 首页
    },
    {
        path: "/found",
        name: "found",
        component: () => import("../page/found/found.vue")   //商品交易
    },
    {
        path: "/game",
        name: "game",
        component: () => import("../page/game/game.vue")    //账号求购
    },
    {
        path: "/material",
        name: "material",
        component: () => import("../page/material/material.vue")   //材料 
    },
    {
        path: "/money",
        name: "money",
        component: () => import("../page/money/money.vue")   //金币
    },
    {
        path: "/computer",
        name: "computer",
        component: () => import("../page/computer/computer.vue")   //金币
    },
    {
        path: "/home",
        name: "home",
        component: () => import("../page/home/home.vue"),   //个人中心
        meta: { requiresAuth: true } // 需要登录才能访问
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
    const isLoggedIn = localStorage.getItem('isLoggedIn');

    if (to.matched.some(record => record.meta.requiresAuth)) {
        if (!isLoggedIn) {
            next('/home/login'); // 未登录，跳转到登录页面
        } else {
            next(); // 已登录，继续访问
        }
    } else {
        next(); // 不需要登录，继续访问
    }
});

export default router;
